/*
 *  AMGlobal.h
 *  AMPlugin
 *
 *  Created by Tiet Anh Vu on 5/30/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "ILayoutUIUtils.h" // InDesign Class
#include "IHierarchy.h"
#include "IDataBase.h"
#include "IMultiColumnTextFrame.h"
#include "IDocument.h"
#include "Magazine.h"
#include "ProgressBar.h"
#include <UIDRef.h>
#include "IDialogMgr.h"

#include "FrmLblID.h"
#include "FrmLblType.h"
#include "IFrmLblDataFacade.h"

#define ROOT_NODE 99
#define MAG_NODE 100
#define ISSUE_NODE 101
#define PAGE_NODE 102
#define STORY_NODE 103
#define NEXT_BTN_SIG 1
#define PREV_BTN_SIG -1

#define NODE_UNSELECTED 200
#define NODE_SELECTED   201
#define NODE_NOTEDITING	208
#define NODE_EDITING	209

#define kCancelSystem	2
#define kOKSystem		1
#define kUnlockSystem	3
#define AMPCheckin		""
#define _INDESIG_DEBUG	1
#define AMUnknown		-1
#define AMCannotOpen	99
#define AMPJPEG_FORMAT	"JPEG"
#define AMPIDML_FORMAT	"InDesignMarkup"
#define AMPICML_FORMAT  "InCopyMarkup"

#define kAMPluginSignInStr				PMString("Sign in")
#define kAMPluginSignOutStr				PMString("Sign out")
#define kAMPluginBrowseIssueStr			PMString("Browse Issues")
#define kAMPluginCheckInStr				PMString("Check in")
#define kAMPluginCheckInStoryStr		PMString("Check in story")
#define kAMPluginCheckOutStoryStr		PMString("Status icon")

//#define API_PREFIX "http://192.168.2.148/api/amp/v1/indesign";
//#define API_PREFIX "api/amp/v1/indesign";
#define API_PREFIX "api/amp/v2/indesign";
#define WORKSPACE_DIR "AMPInDesignPlugin";
//#define API_URLLOGIN  "/api/amp/v1/indesign/login"
#define API_URLLOGIN  "/api/amp/v2/indesign/login"
#define MediaLib	  "MediaLib"
#define PackageLocked "Package is locked"
#define PagesLocked "Pages are locked"
#define kUnlockPackage	"Unlock"
#define NODESIZE_MAXIMUM		200
#define READY_FOR_CHECKOUT			1
#define LOCKED_DATA_PLUGIN_CMS		2
#define UNPULISH_DATA_FOR_CHECKOUT	0
#define NEED_UPDATE_TO_CMS			3
//#import "CrashReporter.h"
//============
// text frame structure
struct textframes_t {
	string textframeUID;
	string storyID;
	bool isLocked;
	string sessionID;
	string storyName;
	int beginRemovePos;
	int endRemovePos;
	////////////////////////////
	string storySelf;
	string attribute;
	//int beginRemoveSelf;
	//int endRemoveSelf;
	////////////////////////////
};
//============
struct assignee_links_t {
	string path_img;
	bool   status_img;
};
//============
// image structure
struct images_t {
	string story_id;
	string image_name;
	string image_caption;
	string image_status;
	string image_path;
};

struct links_t {
	string image_before;
	string image_after;
	string original_path;
};
//============
struct caption_t {
	string caption_self;
};

struct assignee_story_t {
	string storyID;
	string storyName;
};

struct pt_assignStr {
	UIDRef story_assignment;
	UIDRef frameUIDRef;
	std::string story_id;
	int  _status;
	string storyUID;
};

extern vector<pt_assignStr> g_assignmentPtr;
extern vector<caption_t> g_captionImg;
extern vector<assignee_story_t> g_assigneeSt;
//============
/*
typedef struct pageInfo {
	string name_page;
	int    type_page;
}pageInfo, *g_pageInfo;

typedef struct MembersPageInfo {
	g_pageInfo *g_pageLocation;
	int page_number;	//page number
	int page_map;		//page size
}MembersPageInfo, *g_MembersPageInfo;
*/
//============
extern Magazine *g_Magazines;
extern int g_Magazine_Count;
//extern string g_WorkspaceDir;
extern string g_SDK_WorkspaceDir;
extern string g_System_WorkspaceDir;
extern string g_Domain;
extern string g_Username;
extern string g_Password;
extern bool g_IsLoggedIn;
//extern bool g_IsCheckOut;
//extern bool g_ShouldDownload;
//extern bool b_SystemWait;
extern bool g_TextSelected;
extern bool g_PrevTextSelected;
extern string g_SelectedTextFrameUID;
//extern string g_TextFramesID[1000];
extern int g_TextFrameCount;
extern struct textframes_t *g_TextFrames;
extern std::vector<images_t> g_ImageInfo;
extern std::vector<links_t> g_LinkInfo;
extern std::vector<assignee_links_t> g_AssigneeInfo;
//extern UIDRef g_PackageFrameRef;
// Current id check out and check in
extern string g_Magazineid;
extern string g_Issueid;
extern string g_Pageid;
extern string g_Storyid;
extern string g_IDMLzip;
extern string g_Package_id;
extern string g_DocName;
//extern string g_IDMLOpen;
//extern bool g_ShouldUpdateDocName;
//extern bool g_IsIssueEnable;
extern string n_mSessionConnection;
extern string g_System_logDirectory;
extern string g_SDK_logDirectory;
extern string n_mSessionID;				// Session id :
extern int32  n_mSessionTimeOut;		// Session time out :
extern string n_mSessionUserID;			// Session user id :
extern int32  n_mSessionCounterReset;			// Session Counter Reset :
extern int32  n_mSessionTotalCounterReset;		// Session Total Counter Reset :
extern string g_Resolution;
extern string g_Authentication;
extern string g_ChosenStoryID;
extern string g_ChosenImagePath;
extern string g_ChosenImageName;

extern string g_pageNumberCheckOut;
extern string g_pageEditing;
extern string g_issueEditing;
//extern string g_IssueNumberCheckOut;
extern string g_ImagePreview;
//extern int curImgPos;
//extern string curNodeImgPath;
//extern string curNodeName;
extern string g_pageCheckIn;

extern int g_CurPreviewImgPos;
extern string g_CurPreviewImaPath;
extern string g_CurPreviewNodeName;
extern string g_UnlockStories;
extern bool	  g_ProxyEnable;
extern bool   g_ProxyAuthEnable;
extern string g_ProxyPort;
extern string g_ProxyServer;
extern string g_ProxyAuthen;
extern string g_ProxyUser;
extern string g_ProxyPass;

extern int32 g_startTime;
extern int32 g_EndTime;

extern bool	g_CanCheckIn;
extern string	g_AvailableStory;
extern int32	HandleSelectedStatusFrame;
class AMPUtils {
public:
	static int CreateWorkspaceDir();
	static void RefreshMainPanel(int32 m_nPanelContrl);
	static void CallJSDialog();
	static void GetIDFromTextFrame(UIDRef docRef);
	static void GetIDFromTextFrame();
	static bool SetIDToTextFrame(UIDRef docRef);
	static bool SetIDToTextFrame();
	static string getNameFromURL(const string &url);
	static void getid(const string &urlStr);
	static void PostStoryToCMS();
	static void ImportStoryFromCMS();
	static void SaveDocument();
	static void SaveDocument(UIDRef docRef);
	static void RefreshBrowseIssue();
	static void UpdateDocName(UIDRef docRef);
	static void ResetDocName(UIDRef docRef);
	static bool IsCheckedOutDoc(UIDRef docRef);
	static bool CheckInAllStories();
	static bool CheckInAllStories(UIDRef docRef);
	static bool CheckInLastSelectedStory();
    static ErrorCode ExportStoryToICML(const UIDRef& storyUIDRef, const string icmlName);	
	static string FrontDocName(UIDRef docRef = UIDRef::gNull);
	static bool UnlockStories();
	static bool UnlockPackage();
	static bool ClearAllFile();
	static void InitSessionCounter();
	static void RenewLogFilePath();
	static int  RenewWorkspaceDir();
	static bool RenewSessionID(string Session_id);
	static bool SetNameDocument(string nameID);
	static bool ClearPackageAndSessionInfo();
	static bool ClearUserInfo();
	static void RemoveIDFromTextFrames();
	static void RemoveIDFromTextFrames(UIDRef docRef);
	static void HandleQuickSortString(string image_nodename_checkout, string image_path_checkout, string page_check_out);
	
	static void AddStoriesToUnlockQueue();
	static void AddPackagesToUnlockQueue();
	static bool UnlockStoriesInQueue();
	static bool UnlockPackagesInQueue();

//	static ErrorCode ImportIDML(const string idmlName);
	static void CloseCurDocument();
	static bool FindFrontDocsName(PMString FrontName);
	static bool CheckinWhenLogout();
	static bool StoryInfoHiddenSetting(bool finish = kFalse);
	static void GetStoryInfoContent(UIDRef storyInfo);
	static void ResetFrontNameDoc(string Name);
	static void ResetLogFile();
	static void RemoveInfoTextframe();
	static void IssueListRefresh();
	static UIDRef GetIDTextFrameInfo(IDocument *iDocument, string &content);
	static void DoExportIDML();
	static void QueryFrameRef(IDocument *iDocument);
	//static bool QueryLinkImage(IDocument* document);
	static string CalculateTimer(int Startup);
	static void TestIconFrame(int32 status);
	static void AddToAssignment();
	static void UpdateStatusProperties(UIDRef& frameUIDRef, int32 status);
	static UIDRef QueryPreviousGraphicFrame(const UIDRef& graphicFrameUIDRef);
	static bool8 PostCaptionImg(UIDRef captionUIDRef, string caption_name);
	static void SpreadInfoCaption(UIDRef docRef);
	
private:
	static void volumeNameForPath(const char *path, char **volname);
	static void InspectThreadedTextFrames(const UIDRef& graphicFrameUIDRef, FrmLblInfo& frmLblInfo);
	static UIDRef GetPreviousGraphicFrame(const UIDRef& graphicFrameUIDRef);
	static UIDRef GetNextGraphicFrame(const UIDRef& graphicFrameUIDRef);
	static UIDRef GetGraphicFrameRef(const InterfacePtr<ITextFrameColumn>& textFrameColumn, const bool16 isTOPFrameAllowed);
	static UID	  GetTextContentUID(const UIDRef& graphicFrameUIDRef);

};